package com.hzzc.sso.sdk.session;

import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
public class SessionExpireChecker extends Thread {

    private long duration;

    private Map<String, SsoSession> targetCheckSessions;

    public SessionExpireChecker(long duration, Map<String, SsoSession> targetCheckSessions) {
        super("Session过期主动监测线程");
        this.duration = duration;
        this.targetCheckSessions = targetCheckSessions;
    }

    @Override
    public void run() {
        while (true) {
            try {
                targetCheckSessions.forEach((k, v) -> {
                    if (v.isExpired(duration)) {
                        log.info("##Session【{}】已经过期,开始清理", k);
                        targetCheckSessions.remove(k);
                    }
                });
            } catch (Exception e) {
                log.error("##检测Session过期处理异常", e);
            } finally {
                try {
                    Thread.sleep(1000 * 60L);
                } catch (Exception e) {
                    log.error("##检测Session过期处理线程休眠异常！", e);
                }
            }


        }
    }

}
